#!/bin/sh
# postinst script for emqx
#
# see: dh_installdeb(1)

set -e

# create group
if ! getent group emqx >/dev/null; then
        addgroup --system emqx
fi

# create user
if ! getent passwd emqx >/dev/null; then
        adduser --ingroup emqx \
                --home /var/lib/emqx \
                --disabled-password \
                --system --shell /bin/bash --no-create-home \
                --gecos "emqx user" emqx
fi

for i in lib run log; do
    chown -R emqx:emqx /var/$i/emqx
done

chown -R emqx:emqx /usr/lib/emqx
chown -R emqx:emqx /etc/emqx

chmod 0755 /var/run/emqx /etc/emqx
chmod 0644 /etc/emqx/*
chmod -R +X /etc/emqx
chmod -R 0755 /usr/lib/emqx/bin
[ -f /usr/bin/emqx ] && rm /usr/bin/emqx
[ -f /usr/bin/emqx_ctl ] && rm /usr/bin/emqx_ctl
ln -s /usr/lib/emqx/bin/emqx /usr/bin/emqx
ln -s /usr/lib/emqx/bin/emqx_ctl /usr/bin/emqx_ctl

if systemctl status --no-pager; then
    systemctl enable emqx;
else
    echo "systemd is not in use, skip enable emqx"
fi

case "$1" in
    configure)
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
